package com.sinosoft.brokerchannel.server;

import com.sinosoft.brokerchannel.base.bins.AppImageCloudStorage;
import com.sinosoft.brokerchannel.base.common.SystemParamFinal;
import com.sinosoft.brokerchannel.base.dao.AppImageCloudStorageMapper;
import com.sinosoft.brokerchannel.base.utile.BusinessException;
import com.sinosoft.brokerchannel.entity.vo.ImageVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

/**
 * @需求名称:图像云存储
 * @类描述:图片云存储的service
 */
@Service
public class AppImageCloudStorageService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    @Qualifier("appImageCloudStorageMapper")
    private AppImageCloudStorageMapper appImageCloudStorageMapper;

    /**
     * @param imageVO
     * @throws BusinessException
     * @需求名称：图片云存储
     * @描述:对图片的操作：上传、下载、删除
     */
    public void storage(ImageVO imageVO) throws BusinessException {
        try {
            String actionType = imageVO.getActionType();
            if ((SystemParamFinal.IMAGE_UPLOAD).equals(actionType)) {//上传
                this.imageUpload(imageVO);
            } else if ((SystemParamFinal.IMAGE_DELETE).equals(actionType)) {//删除
                this.imageDelete(imageVO);
            }
        } catch (Exception e) {
            logger.info(SystemParamFinal.Exception_LOG, e);
            throw new BusinessException("0061", "图片上传失败");
        }
    }

    /**
     * @param imageVO({@link ImageVO})
     * @throws BusinessException
     * @需求名称：图片云存储
     * @描述:图片上传
     * @创建单位:中科软
     * @创建日期:2017年9月26日
     * @参数说明：
     */
    private void imageUpload(ImageVO imageVO) throws BusinessException {
        try {
            if (imageVO != null) {
                AppImageCloudStorage record = new AppImageCloudStorage();
                record.setAppno(imageVO.getcAppNo());//投保单号
                record.setPlyno(imageVO.getcPlyNo());//保单号
                record.setImgtyp(imageVO.getcImgTyp());//图片类型
                record.setApptype(imageVO.getcAppTyp());//上传类型
                record.setActiontype(imageVO.getActionType());//操作类型
                record.setFileid(imageVO.getFileId());//文件ID
                record.setDownloadurl(imageVO.getDownloadUrl());//下载URL
                record.setUrl(imageVO.getUrl());//打开URL
                record.setStatus(imageVO.getStatus());//状态
                record.setMessage(imageVO.getResponseMessag());//提示信息
                record.setFlag(SystemParamFinal.FLAG_EFFCTIVE);// 1-有效
                appImageCloudStorageMapper.insert(record);
            }
        } catch (Exception e) {
            logger.info(SystemParamFinal.Exception_LOG, e);
            throw new BusinessException("0061", "图片上传失败");
        }
    }

    /**
     * @param imageVO
     * @throws BusinessException
     * @需求名称：图片云存储
     * @描述:图片删除
     */
    private void imageDelete(ImageVO imageVO) throws BusinessException {
        try {
            if (imageVO != null && imageVO.getFileId() != null) {
                String fileId = imageVO.getFileId();
                AppImageCloudStorage record = appImageCloudStorageMapper.selectByPrimaryKey(fileId);
                record.setFlag(SystemParamFinal.FLAG_UNEFFECTIVE);//0-无效
                record.setStatus(imageVO.getStatus());
                record.setMessage(imageVO.getResponseMessag());
                appImageCloudStorageMapper.updateByPrimaryKeySelective(record);
            }
        } catch (Exception e) {
            logger.info(SystemParamFinal.Exception_LOG, e);
            throw new BusinessException("0060", "图片删除失败");
        }
    }
}
